package com.dwxt.community.admin.service;

import com.dwxt.common.base.BaseResult;
import com.dwxt.common.base.StatusCode;
import com.dwxt.community.dao.EditContentMapper;
import com.dwxt.community.entity.EditContent;
import com.dwxt.community.entity.EditContentExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jdk.nashorn.internal.runtime.logging.Logger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class EditContentService {

    @Autowired
    private EditContentMapper editContentMapper;

    public BaseResult selectList(Integer pageSize, Integer pageNum){
        PageHelper.startPage(pageNum, pageSize);
        return BaseResult.ok(new PageInfo<>(editContentMapper.selectByExample(new EditContentExample())));
    }
    public BaseResult insertOrUpdate(EditContent editContent){
        //修改
        int i;
        EditContentExample editContentExample = new EditContentExample();
        editContentExample.createCriteria().andEditNameEqualTo(editContent.getEditName());
        List<EditContent> editContents = editContentMapper.selectByExample(editContentExample);
        if (editContents.size()>1){
            //log.info("1");
            return BaseResult.build(StatusCode.INFO_IS_EXIST);
        }
        if (editContent.getId()!=null && editContents.size()==1){
            //log.info("2");
            editContentMapper.updateByPrimaryKeySelective(editContent);
        }else if(editContents.size()==0){
            //log.info("3");
            editContentMapper.insertSelective(editContent);
        }
        return BaseResult.ok();
    }

    public void delete(Integer id){
        editContentMapper.deleteByPrimaryKey(id);
    }

    public BaseResult selectAll() {
        return BaseResult.ok(editContentMapper.selectByExample(new EditContentExample()));
    }
}
